The human parasitic nematode Strongyloides stercoralis is estimated to infect approximately 610 million people (Buonfrate et al 2020). Similar to other intestinal parasitic nematodes, S. stercoralis has a complex life cycle in which distinct developmental stages navigate starkly different environments via life-stage specific behavioral preferences ( Castelletto et al 2014, Bryant and Hallem 2018a ). Across nematode species, ethological and behavioral differences are often reflected in the temporal regulation of gene expression. For S. stercoralis, several studies have utilized bulk RNA sequencing to probe the genomic basis of parasitism by identifying gene families that are uniquely upregulated in parasitic life stages ( Stolzfus et al 2012, Hunt et al 2016, Hunt et al 2018). These efforts highlight a feature of modern bioinformatics - the secondary analysis of publically avaliable datasets. Here, an original dataset featuring bulk RNA sequencing of seven S. stercoralis developmental stages was initially aligned to genomic contigs (6 December 2011 draft, Stolzfus et al 2012 ). Subsequently, a subset of this dataset was re-analyzed coincident with the release of a high-quality draft assembly ( Hunt et al 2016 ); this re-analysis focused on the differences between three life stages: free-living adults that navigate the environment, parasitic adults located within the host intestine, and infective third-stage larvae that actively engage in host-seeking behaviors. As genome assembly and RNA sequencing of additional parasitic nematode species continues, the publically-avaliable S. stercoralis RNAseq dataset continues to be utilized for cross-species and cross-life stage comparisons ( Hunt et al 2016, Hunt et al 2018 ). Furthermore, several helminth RNA-seq datasets, including S. stercoralis were realigned to their reference genomes and integrated into WormBase Parasite, where they are accessible to the field at large in the form of genome-aligned RNA-seq expression tracks ( Howe et al 2017 ).
As research into the genomic basis of parasitism in helminths continues, access to quantitative gene expression levels will greatly enhance studies into the functional role of specific genes and gene families. Differential gene expression results have been published as supplemental data ( Hunt et al 2016 ), however these analyses only included pairwise comparisons between three life stages. Quantitative comparisons that utilize the full seven avaliable life stages have not yet been published.
A description of Kallisto alignment and data filtering/normalization steps can be found in Ss_RNAseq_Data_Processing.rmd.
Code included in this section is not (yet) available as part of the Shiny Web App
Principal component analysis applied to filtered and TMM-normalized expression data identified two developmental clusters that account for 42.6% and 28.9% of expression variability between S. stercoralis developmental stages. The top 10% of genes contributing to PC1 and PC2 are identified and printed as a DataTable.
The limma package ( Ritchie et al 2015, Phipson et al 2016) is used to conduct pairwise differential gene expression analyses between life stages. Here, all unique pairwise comparisons between life stages are displayed as interactive volcano plots and DataTables. In this analysis, genes included in DataTables are corrected for multiple life-stage comparisons. An additional code chunk identifies all genes that are differentially expressed in any way across groups (i.e. an ANOVA analysis across life stages). This set of genes is passed into a clustering analysis that generates a heatmap of differential gene expression across life stages.
load (file = "../Outputs/SsRNAseq_data_preprocessed")
targets <- SsRNAseq.preprocessed.data$targets
annotations <- SsRNAseq.preprocessed.data$annotations
log2.cpm.filtered.norm <- SsRNAseq.preprocessed.data$log2.cpm.filtered.norm
myDGEList.filtered.norm <-SsRNAseq.preprocessed.data$myDGEList.filtered.norm
rm(SsRNAseq.preprocessed.data)
load(file = "../Outputs/vDEGList")
# Introduction to this chunk -----------
# This code chunk starts with filtered and normalized abundance data in a data frame (not tidy).
# It will implement hierarchical clustering and PCA analyses on the data.
# It will plot various graphs and can save them in PDF files.
# Load packages ------
suppressPackageStartupMessages({
library(tidyverse) # you're familiar with this fromt the past two lectures
library(ggplot2)
library(RColorBrewer)
library(ggdendro)
library(magrittr)
library(factoextra)
library(gridExtra)
library(cowplot)
library(dendextend)
})
# Identify variables of interest in study design file ----
group <- factor(targets$group)
# Hierarchical clustering ---------------
# Remember: hierarchical clustering can only work on a data matrix, not a data frame
# Calculate distance matrix
# dist calculates distance between rows, so transpose data so that we get distance between samples.
# how similar are samples from each other
colnames(log2.cpm.filtered.norm)<-targets$group
distance <- dist(t(log2.cpm.filtered.norm), method = "maximum") #other distance methods are "euclidean", maximum", "manhattan", "canberra", "binary" or "minkowski"
# Calculate clusters to visualize differences. This is the hierarchical clustering.
# The methods here include: single (i.e. "friends-of-friends"), complete (i.e. complete linkage), and average (i.e. UPGMA). Here's a comparison of different types: https://en.wikipedia.org/wiki/UPGMA#Comparison_with_other_linkages
clusters <- hclust(distance, method = "complete") #other agglomeration methods are "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", or "centroid"
dend <- as.dendrogram(clusters)
p1<-dend %>%
dendextend::set("branches_k_color", k = 5) %>%
dendextend::set("hang_leaves", c(0.1)) %>%
dendextend::set("labels_cex", c(0.5)) %>%
dendextend::set("labels_colors", k = 5) %>%
dendextend::set("branches_lwd", c(0.7)) %>%
as.ggdend %>%
ggplot (offset_labels = -0.5) +
theme_dendro() +
ylim(0, max(get_branches_heights(dend))) +
labs(title = "Hierarchical Cluster Dendrogram ",
subtitle = "filtered, TMM normalized",
y = "Distance",
x = "Life stage") +
coord_fixed(1/2) +
theme(axis.title.x = element_text(color = "black"),
axis.title.y = element_text(angle = 90),
axis.text.y = element_text(angle = 0),
axis.line.y = element_line(color = "black"),
axis.ticks.y = element_line(color = "black"),
axis.ticks.length.y = unit(2, "mm"))
p1
# Principal component analysis (PCA) -------------
# this also works on a data matrix, not a data frame
pca.res <- prcomp(t(log2.cpm.filtered.norm), scale.=F, retx=T)
summary(pca.res) # Prints variance summary for all principal components.
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 124.8775 105.7601 60.28878 53.37388 41.40321 26.13575
## Proportion of Variance 0.4219 0.3026 0.09833 0.07707 0.04638 0.01848
## Cumulative Proportion 0.4219 0.7245 0.82282 0.89989 0.94627 0.96475
## PC7 PC8 PC9 PC10 PC11 PC12
## Standard deviation 20.28184 13.66342 10.37244 9.93966 9.00105 8.75746
## Proportion of Variance 0.01113 0.00505 0.00291 0.00267 0.00219 0.00207
## Cumulative Proportion 0.97588 0.98093 0.98384 0.98651 0.98870 0.99078
## PC13 PC14 PC15 PC16 PC17 PC18 PC19
## Standard deviation 7.89616 7.37748 7.04007 6.52221 6.41971 6.00467 5.58038
## Proportion of Variance 0.00169 0.00147 0.00134 0.00115 0.00111 0.00098 0.00084
## Cumulative Proportion 0.99246 0.99394 0.99528 0.99643 0.99754 0.99852 0.99936
## PC20 PC21
## Standard deviation 4.85766 6.462e-14
## Proportion of Variance 0.00064 0.000e+00
## Cumulative Proportion 1.00000 1.000e+00
#pca.res$rotation #$rotation shows you how much each gene influenced each PC (called 'scores')
#pca.res$x # 'x' shows you how much each sample influenced each PC (called 'loadings')
#note that these have a magnitude and a direction (this is the basis for making a PCA plot)
## This generates a screeplot: a standard way to view eigenvalues for each PCA. Shows the proportion of variance accounted for by each PC. Plotting only the first 10 dimensions.
p2<-fviz_eig(pca.res,
barcolor = brewer.pal(8,"Pastel2")[8],
barfill = brewer.pal(8,"Pastel2")[8],
linecolor = "black",
main = "Scree plot: proportion of variance accounted for by each principal component",
ggtheme = theme_bw())
p2
pc.var<-pca.res$sdev^2 # sdev^2 captures these eigenvalues from the PCA result
pc.per<-round(pc.var/sum(pc.var)*100, 1) # we can then use these eigenvalues to calculate the percentage variance explained by each PC
# Visualize the PCA result ------------------
#lets first plot any two PCs against each other
#We know how much each sample contributes to each PC (loadings), so let's plot
pca.res.df <- as_tibble(pca.res$x)
# Plotting PC1 and PC2
p3<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
fill = targets$group,
color = targets$group
) +
geom_point(size=4, shape= 21, color = "black", alpha = 0.5) +
#geom_label(color = "black", size = 2) +
scale_fill_brewer(palette = "Set2") +
scale_color_brewer(palette = "Set2", guide = FALSE) +
#stat_ellipse() +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(title="Principal Components Analysis of S. stercoralis RNAseq Samples",
sub = "Note: analysis is blind to life stage identity.",
fill = "Life Stage") +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
p3
# A guess at the identity of PC1
p4<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
color = factor(targets$Maturity),
fill = factor(targets$Maturity)
) +
#geom_point(size=4) +
#stat_ellipse() +
geom_label(color = "black", size = 2, alpha = 0.7) +
scale_fill_manual(values = brewer.pal(8,"Set2")) +
scale_color_manual(values = brewer.pal(8,"Set2"), guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(subtitle="Potential PC1 identity: Adults vs Larvae",
fill = "Maturity") +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
p4
# A guess at the identity of PC2
p5<-ggplot(pca.res.df) +
aes(x=PC1, y=PC2, label=targets$group,
color = factor(targets$Infectious),
fill = factor(targets$Infectious)
) +
#geom_point(size=4) +
#stat_ellipse() +
geom_label(color = "black", size = 2, alpha = 0.7) +
scale_fill_manual(values = brewer.pal(8,"Set2")[3:4]) +
scale_color_manual(values = brewer.pal(8,"Set2")[3:4], guide = FALSE) +
xlab(paste0("PC1 (",pc.per[1],"%",")")) +
ylab(paste0("PC2 (",pc.per[2],"%",")")) +
labs(subtitle="Potential PC2 identity: Host-seeking/dwelling",
fill = 'Infectivity Stage') +
scale_x_continuous(expand = c(.3, .3)) +
scale_y_continuous(expand = c(.3, .3)) +
coord_fixed() +
theme_bw()
p5
# Create a PCA 'small multiples' chart ----
pca.res.df <- pca.res$x[,1:6] %>%
as_tibble() %>%
add_column(sample = targets$sample,
group = group,
maturity = factor(targets$Maturity),
infectious = factor(targets$Infectious))
pca.pivot <- pivot_longer(pca.res.df, # dataframe to be pivoted
cols = PC1:PC6, # column names to be stored as a SINGLE variable
names_to = "PC", # name of that new variable (column)
values_to = "loadings") # name of new variable (column) storing all the values (data)
PC1<-subset(pca.pivot, PC == "PC1")
PC2 <-subset(pca.pivot, PC == "PC2")
#PC3 <- subset(pca.pivot, PC == "PC3")
#PC4 <- subset(pca.pivot, PC == "PC4")
p6<-ggplot(pca.pivot) +
aes(x=sample, y=loadings) + # you could iteratively 'paint' different covariates onto this plot using the 'fill' aes
geom_bar(stat="identity", fill = brewer.pal(8,"Pastel2")[8]) +
scale_fill_brewer(palette = "Set2") +
facet_wrap(~PC) +
geom_bar(data = PC1, stat = "identity", aes(fill = maturity)) +
geom_bar(data = PC2, stat = "identity", aes(fill = infectious)) +
labs(title="PCA 'small multiples' plot",
fill = "Life Stage Groups",
caption=paste0("produced on ", Sys.time())) +
scale_x_discrete(limits = targets$sample, labels = targets$group) +
theme_bw() +
coord_flip()
p6
# Graph all the plots generated in this script ----
# Plot all the plots on a grid
# p7<- grid.arrange(p1,p2,p3,p4, p5,p6,ncol = 4,
# layout_matrix = cbind(c(1,1,1,2,2,2),c(3,3,4,4,5,5),c(6,6,6,6,6,6), c(6,6,6,6,6,6)))
# ggsave("Strongyloides stercoralis RNAseq Multivariate Analysis.pdf",
# plot = p7,
# device = "pdf",
# #height = 17,
# width = 22,
# path = '../Outputs/'
# Introduction to this chunk ----
# This chunk provides additional analysis of the principal components, in order to determine which genes are influencing the identified PCs.
# Use pca.res$rotation to select genes influencing PC1-6 ----
myscores.df <- pca.res$rotation[,1:6] %>%
as_tibble(rownames = "geneID") %>%
pivot_longer(cols = -geneID, names_to = "PC", values_to = "scores") %>%
dplyr::mutate(abs_scores = abs(scores)) %>%
group_by(PC) %>%
slice_max(abs_scores, prop = .1) # get top 10% of genes in all PCs
# Pull out genes that are the top 10% of contributors (in any direction) to PC1 and PC2. Annotate.
myscores.Top10 <- myscores.df %>%
dplyr::filter(PC == "PC1" | PC == "PC2") %>%
pivot_wider(id_cols = geneID,
names_from = PC,
values_from = scores) %>%
dplyr::mutate(PC1_id = case_when(PC1 > 0 ~ "larval",
PC1 < 0 ~ "adult",
is.na(PC1) ~ "--")) %>%
dplyr::mutate(PC2_id = case_when(PC2 > 0 ~ "parasite",
PC2 < 0 ~ "non_parasite",
is.na(PC2) ~ "--")) %>%
dplyr::left_join(.,(rownames_to_column(annotations, var = "geneID")), by = "geneID") %>%
dplyr::relocate(UniProtKB, Description, InterPro, GO_term, Ce_geneID, percent_homology, .after = PC2_id)
# Make Interactive Plot
myscores.Top10.interactive <- myscores.Top10 %>%
DT::datatable(extensions = c('KeyTable', "FixedHeader"),
rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left;',
htmltools::tags$b('Top 10% of Genes Contributing to PC1 and PC2'),
htmltools::tags$br(),
'Search for PC1_id/PC2_id values to filter results'),
options = list(keys = TRUE,
autoWidth = TRUE,
scrollX = TRUE,
order = list(1, 'desc'),
searchHighlight = TRUE,
pageLength = 10,
lengthMenu = c("10", "25", "50", "100"))) %>%
DT::formatRound(columns=c(2:3), digits=3)
myscores.Top10.interactive
# Introduction to this chunk ----
# This chunk uses a variance-stabilized DGEList of filtered and normalized abundance data.
#
# These data/results are examples, a responsive version of this code is avaliable in a Shiny App.
#
# Because we have access to biological replicates, we can use statistical tools for differential expression analysis
# Useful reading on differential expression: https://ucdavis-bioinformatics-training.github.io/2018-June-RNA-Seq-Workshop/thursday/DE.html
# Load packages ----
suppressPackageStartupMessages({
library(tidyverse)
library(limma) # differential gene expression using linear modeling
library(edgeR)
library(gt)
library(DT)
library(plotly)
library(ggthemes)
library(RColorBrewer)
source("../Stercoralis_RNAseq_Browser/Server/theme_Publication.R")
})
diffGenes.df <- v.DEGList.filtered.norm$E %>%
as_tibble(rownames = "geneID", .name_repair = "unique")
# Set Expression threshold values for plotting and saving DEGs ----
adj.P.thresh <- 0.05
lfc.thresh <- 1
group <- factor(targets$group)
design <- model.matrix(~0 + group) # no intercept/blocking for matrix, comparisons across group
colnames(design) <- levels(group)
# Fit a linear model to the data ----
fit <- lmFit(v.DEGList.filtered.norm, design)
# As an example, generate comparison matrix for two pairwise comparisons ----
# iL3s vs FLF and iL3s vs iL3+s
# Note that the target/contrast goups will be divided by the number of life
# stage groups e.g. PF+FLF/2 - iL3+iL3a+pfL1+ppL1+ppL3/5
comparison <- c('(iL3)-(FLF)',
'(PF)-(FLF)')
targetStage<- comparison %>%
str_split(pattern="-", simplify = T) %>%
.[,1] %>%
gsub("(", "", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
str_split(pattern = "\\+", simplify = T)
contrastStage<-comparison %>%
str_split(pattern="-", simplify = T) %>%
.[,2] %>%
gsub("(", "", ., fixed = TRUE) %>%
gsub(")", "", ., fixed = TRUE) %>%
str_split(pattern = "\\+", simplify = T)
comparison<- sapply(seq_along(comparison),function(x){
tS <- as.vector(targetStage[x,]) %>%
.[. != ""]
cS <- as.vector(contrastStage[x,]) %>%
.[. != ""]
paste(paste0(tS,
collapse = "+") %>%
paste0("(",.,")/",length(tS)),
paste0(cS,
collapse = "+") %>%
paste0("(",.,")/",length(cS)),
sep = "-")
})
# Generate contrast matrix ----
contrast.matrix <- makeContrasts(contrasts = comparison,
levels=design)
# extract the linear model fit -----
fits <- contrasts.fit(fit, contrast.matrix)
# empirical bayes smoothing of gene-wise standard deviations provides increased power (see: https://www.degruyter.com/doi/10.2202/1544-6115.1027)
ebFit <- eBayes(fits)
# Pull out the DEGs that pass a specific threshold for all pairwise comparisons ----
# Adjust for multiple comparisons using method = global.
results <- decideTests(ebFit, method="global", adjust.method="BH", p.value = adj.P.thresh)
recode01<- function(x){
case_when(x == 1 ~ "Up",
x == -1 ~ "Down",
x == 0 ~ "NotSig")
}
diffDesc <- results %>%
as_tibble(rownames = "geneID") %>%
dplyr::mutate(across(-geneID, unclass)) %>%
dplyr::mutate(across(where(is.double), recode01))
# Function that identifies top DEGs between a specific contrast ----
calc_DEG_tbl <- function (ebFit, coef) {
myTopHits.df <- limma::topTable(ebFit, adjust ="BH",
coef=coef, number=40000,
sort.by="logFC") %>%
as_tibble(rownames = "geneID") %>%
dplyr::rename(tStatistic = t, LogOdds = B, BH.adj.P.Val = adj.P.Val) %>%
dplyr::relocate(UniProtKB, Description, InterPro, GO_term, Ce_geneID, percent_homology, .after = LogOdds)
myTopHits.df
}
list.myTopHits.df <- sapply(comparison, function(y){
calc_DEG_tbl(ebFit, y)},
simplify = FALSE,
USE.NAMES = TRUE)
list.myTopHits.df <- sapply(comparison, function(y){
list.myTopHits.df[[y]] %>%
dplyr::select(geneID,
logFC,
BH.adj.P.Val:percent_homology)},
simplify = FALSE,
USE.NAMES = TRUE)
# Get log2CPM values and threshold information for genes of interest
list.myTopHits.df <- sapply(seq_along(comparison), function(y){
tS<- targetStage[y,][targetStage[y,]!=""]
cS<- contrastStage[y,][contrastStage[y,]!=""]
concat_name <- function(x) {
ifelse(x == "target",
paste(tS, collapse = "+"),
paste(cS, collapse = "+"))
}
groupAvgs <- diffGenes.df %>%
dplyr::select(geneID, starts_with(paste0(tS,"-")),
starts_with(paste0(cS,"-"))) %>%
pivot_longer(cols = -geneID, names_to = c("group","sample"), values_to = "CPM",
names_sep = "-") %>%
dplyr::mutate(contrastID = if_else(group %in% tS,"target", "contrast")) %>%
group_by(geneID, contrastID) %>%
dplyr::select(-sample) %>%
summarize(mean = mean(CPM), .groups = "drop_last") %>%
pivot_wider(names_from = contrastID, values_from = mean) %>%
dplyr::relocate(contrast, .after = target) %>%
dplyr::rename_with(concat_name, -geneID) %>%
dplyr::rename_with(.cols =-geneID, .fn = ~ paste0("avg_(",.x,")"))
diffGenes.df %>%
dplyr::select(geneID, starts_with(paste0(tS,"-")),
starts_with(paste0(cS,"-"))) %>%
left_join(groupAvgs, by = "geneID") %>%
left_join(list.myTopHits.df[[y]],., by = "geneID") %>%
left_join(dplyr::select(diffDesc,geneID,comparison[y]), by = "geneID") %>%
dplyr::rename(DEG_Desc=comparison[y]) %>%
dplyr::relocate(DEG_Desc) %>%
dplyr::relocate(logFC:percent_homology, .after = last_col())
},
simplify = FALSE)
comparison <- gsub("/[0-9]*","", comparison)
names(list.myTopHits.df) <- comparison
list.myTopHits.df <- sapply(comparison, function(y){
list.myTopHits.df[[y]] %>%
dplyr::mutate(DEG_Desc = case_when(DEG_Desc == "Up" ~ paste0("Up in ", str_split(y,'-',simplify = T)[1,1]),
DEG_Desc == "Down" ~ paste0("Down in ", str_split(y,'-',simplify = T)[1,1]),
DEG_Desc == "NotSig" ~ "NotSig"))
},
simplify = FALSE,
USE.NAMES = TRUE)
# PC1 Volcano Plot and Interactive Table ----
vplot1 <- ggplot(list.myTopHits.df[[1]]) +
aes(y=-log10(BH.adj.P.Val), x=logFC, text = paste(geneID, "<br>",
"logFC:", round(logFC, digits = 2), "<br>",
"p-val:", format(BH.adj.P.Val, digits = 3, scientific = TRUE))) +
geom_point(size=2) +
geom_hline(yintercept = -log10(adj.P.thresh),
linetype="longdash",
colour="grey",
size=1) +
geom_vline(xintercept = lfc.thresh,
linetype="longdash",
colour="#BE684D",
size=1) +
geom_vline(xintercept = -lfc.thresh,
linetype="longdash",
colour="#2C467A",
size=1) +
labs(title = paste0('Pairwise Comparison: ',
gsub('-',
' vs ',
comparison[1])),
subtitle = paste0("grey line: p = ",
adj.P.thresh, "; colored lines: log-fold change = ", lfc.thresh),
color = "GeneIDs") +
theme_Publication()
vplot1
# Interactive Tables
y<- 1
tS<- targetStage[y,][targetStage[y,]!=""]
cS<- contrastStage[y,][contrastStage[y,]!=""]
n_num_cols <- length(tS)*3 + length(cS)*3 + 5
LS.datatable <- list.myTopHits.df[[y]] %>%
DT::datatable(rownames = FALSE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left; color: black',
htmltools::tags$b('Differentially Expressed Genes in',
htmltools::tags$em('S. stercoralis'),
gsub('-',' vs ',comparison[y])),
htmltools::tags$br(),
"Threshold: p < ",
adj.P.thresh, "; log-fold change > ",
lfc.thresh,
htmltools::tags$br(),
'Values = log2 counts per million'),
options = list(autoWidth = TRUE,
scrollX = TRUE,
scrollY = '300px',
scrollCollapse = TRUE,
order = list(n_num_cols-1,
'desc'),
searchHighlight = TRUE,
pageLength = 25,
lengthMenu = c("5",
"10",
"25",
"50",
"100"),
columnDefs = list(
list(
targets = ((n_num_cols +
1)),
render = JS(
"function(data, row) {",
"data.toExponential(1);",
"}")
),
list(
targets = ((n_num_cols +
4):(n_num_cols +
5)),
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data.length > 20 ?",
"'<span title=\"' + data + '\">' + data.substr(0, 20) + '...</span>' : data;",
"}")
),
list(targets = "_all",
class="dt-right")
)
))
LS.datatable <- LS.datatable %>%
DT::formatRound(columns=c(3:n_num_cols),
digits=3)
LS.datatable <- LS.datatable %>%
DT::formatRound(columns=c(n_num_cols+2,
n_num_cols+8),
digits=2)
LS.datatable <- LS.datatable %>%
DT::formatSignif(columns=c(n_num_cols+1),
digits=3)
LS.datatable
# Load packages ----
suppressPackageStartupMessages({
library(tidyverse)
library(limma)
library(openxlsx)
library(gplots) #for heatmaps
library(DT) #interactive and searchable tables of our GSEA results
library(GSEABase) #functions and methods for Gene Set Enrichment Analysis
library(Biobase) #base functions for bioconductor; required by GSEABase
library(GSVA) #Gene Set Variation Analysis, a non-parametric and unsupervised method for estimating variation of gene set enrichment across samples.
library(gprofiler2) #tools for accessing the GO enrichment results using g:Profiler web resources
library(clusterProfiler) # provides a suite of tools for functional enrichment analysis
library(msigdbr) # access to msigdb collections directly within R
library(enrichplot) # great for making the standard GSEA enrichment plots
})
# Pick a pairwise comparison
yy <- 1
# Carry out GO enrichment using gProfiler2 ----
# GO enrichment requires a pre-selected set of genes. Can use multiple criteria to do that initial selection.
# The GO terms I'm accessing using the gost are from Hunt et al 2016, I believe.
# # PC1 TopTable Results
# enriched.set.pos <-list.myTopHits.df[[yy]] %>%
# slice_max(logFC, prop = .1) # get top 10% of genes
#
# enriched.set.neg <- list.myTopHits.df[[yy]] %>%
# slice_min(logFC, prop = .1) # get top 10% of genes
#
# gost.res.pos <- gost(list(Target_Upregulated = enriched.set.pos$geneID), organism = "ststerprjeb528", correction_method = "fdr")
# gostplot(gost.res.pos, interactive = T, capped = T)
#
# gost.res.neg <- gost(list(Target_Downregulated_Genes = enriched.set.neg$geneID), organism = "ststerprjeb528", correction_method = "fdr")
# gostplot(gost.res.neg, interactive = T, capped = T)
# Perform GSEA using clusterProfiler ----
# Which library to use for implementation? As per https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbz158/5722384: "For expression-based EA on the full expression matrix...When given raw read counts, we recommend to apply a VST such as voom [39] to arrive at library-size normalized logCPMs."
# For testing self-contained null hypothesis (test for association of any gene in the set with the phenotype), use ROAST
# For testing competitive null hypothesis (test for excess of differential expression in a gene set relative to genes outside the set) - **their recommendation**, use PADOG or SAFE?
#
# Ability to do this depends on the availability of gene sets. Major databases (e.g. msigdb don't seem to have stercoralis information. They do have C. elegans gene sets, but I'm not convinced the homology information is good enough for the comparison to be unbiased/meaningful.
#
# Although the Lok lab did GSEA in Ramanathan *et al* 2011 (PMID: 21572524), using C. elegans homologs are two manually compiled gene sets:
# 1) 31 genes with products known to be immunoreactive in *S. stercoralis*-infected humans
# 2) 42 putatively identified heat shock proteins
# Of course, this is before the genome was sequenced, so these genes don't have associated SSTP numbers.
#
# In Hunt et al 2016, there is an Ensembl Compara protein family set
# Note that this uses specific transcript information, which I throw out.
# (e.g. SSTP_0001137400.2 is recoded as SSTP_0001137400)
ensComp.geneIDs <- read.xlsx ("../Data/Hunt_Parasite_Ensembl_Compara.xlsx",
sheet = 1) %>%
as_tibble() %>%
dplyr::select(-Family.members) %>%
pivot_longer(cols = -Compara.family.id, values_to = "geneID") %>%
dplyr::select(-name) %>%
dplyr::filter(grepl("SSTP_", geneID))
ensComp.geneIDs$geneID <- str_remove_all(ensComp.geneIDs$geneID, ".[0-9]$")
# Compare these genes to the list of genes in our filtered, normalized list ----
#
compara.exclusive <- unique(ensComp.geneIDs$geneID) %>%
as_tibble_col(column_name = "geneID") %>%
dplyr::anti_join(diffGenes.df, by = "geneID")
nrow(compara.exclusive)
## [1] 443
compara.absent <- unique(ensComp.geneIDs$geneID) %>%
as_tibble_col(column_name = "geneID") %>%
dplyr::anti_join(diffGenes.df,., by = "geneID") %>%
dplyr::select(geneID)
nrow(compara.absent)
## [1] 1357
# How many genes have associated GO terms? ----
GO.present <- list.myTopHits.df[[yy]]$GO_term %>%
gsub("NA", NA,.) %>%
as_tibble_col(column_name = "GO_Term") %>%
tibble(geneID = list.myTopHits.df[[yy]]$geneID,.) %>%
dplyr::filter(!is.na(GO_Term))
nrow(GO.present)
## [1] 7181
# Are any of these genes part of those not found in the compara dataset? ----
GO.present.Compara.absent <- dplyr::semi_join(GO.present, compara.absent, by = "geneID")
nrow(GO.present.Compara.absent)
## [1] 538
# Make a list of genes
ensComp.familyIDs <- read.xlsx ("../Data/Hunt_Parasite_Ensembl_Compara.xlsx",
sheet = 2,
cols = c(1,4:6)) %>%
as_tibble() %>%
dplyr::mutate(Family_Description = dplyr::coalesce(.$Description,
.$`Top.product.(members.with.hit)`,
.$`Interpro.top.hit.(members.with.hit)`)
) %>%
dplyr::select(Compara.family.id, Family_Description)
ensComp <- left_join(ensComp.geneIDs, ensComp.familyIDs, by = "Compara.family.id") %>%
dplyr::select(-Compara.family.id) %>%
dplyr::rename(gs_name = Family_Description, gene_symbol = geneID) %>%
dplyr::relocate(gs_name, gene_symbol)
rm(ensComp.geneIDs, ensComp.familyIDs)
# Generate rank ordered list of genes
mydata.df.sub <- dplyr::select(list.myTopHits.df[[yy]], geneID, logFC)
mydata.gsea <- mydata.df.sub$logFC
names(mydata.gsea) <- as.character(mydata.df.sub$geneID)
mydata.gsea <- sort(mydata.gsea, decreasing = TRUE)
# run GSEA using the 'GSEA' function from clusterProfiler
# Given a priori defined set of gene S (e.g., genes shareing the same DO category), the goal of GSEA is to determine whether the members of S are randomly distributed throughout the ranked gene list (L) or primarily found at the top or bottom.
# There are three key elements of the GSEA method:
# **Calculation of an Enrichment Score.**
# The enrichment score (ES) represent the degree to which a set S is over-represented at the top or bottom of the ranked list L. The score is calculated by walking down the list L, increasing a running-sum statistic when we encounter a gene in S and decreasing when it is not. The magnitude of the increment depends on the gene statistics (e.g., correlation of the gene with phenotype). The ES is the maximum deviation from zero encountered in the random walk; it corresponds to a weighted Kolmogorov-Smirnov-like statistic (Subramanian et al. 2005).
# **Esimation of Significance Level of ES.**
# The p-value of the ES is calculated using permutation test. Specifically, we permute the gene labels of the gene list L and recompute the ES of the gene set for the permutated data, which generate a null distribution for the ES. The p-value of the observed ES is then calculated relative to this null distribution.
# **Adjustment for Multiple Hypothesis Testing.**
# When the entire gene sets were evaluated, DOSE adjust the estimated significance level to account for multiple hypothesis testing and also q-values were calculated for FDR control.
myGSEA.res <- GSEA(mydata.gsea, TERM2GENE=ensComp, verbose=FALSE)
## Warning in fgsea(pathways = geneSets, stats = geneList, nperm = nPerm, minSize = minGSSize, : There are ties in the preranked stats (0.28% of the list).
## The order of those tied genes will be arbitrary, which may produce unexpected results.
myGSEA.df <- as_tibble(myGSEA.res@result)
myGSEA.tbl<-as_tibble(myGSEA.res@result) %>%
dplyr::select(-c(Description, pvalue, enrichmentScore)) %>%
dplyr::rename(normalized_EnrichmentScore = NES)
# view results as an interactive table
enrichment.DT <- datatable(myGSEA.tbl,
rownames = TRUE,
caption = htmltools::tags$caption(
style = 'caption-side: top; text-align: left; color: black',
htmltools::tags$b('Gene Families Enriched in iL3 vs FLF')),
options = list(
autoWidth = TRUE,
scrollX = TRUE,
scrollY = '800px',
scrollCollapse = TRUE,
searchHighlight = TRUE,
order = list(3, 'desc'),
pageLength = 25,
lengthMenu = c("5",
"10",
"25",
"50",
"100"),
columnDefs = list(
# list(
# targets = ((7)),
# render = JS(
# "function(data, row) {",
# "data.toExponential(1);",
# "}")
# ),
list(targets = "_all",
class="dt-right")))) %>%
formatRound(columns=c(3,5:6), digits=2) %>%
formatRound(columns=c(4), digits=4)
enrichment.DT
# create enrichment plots using the enrichplot package
# gseaplot2(myGSEA.res,
# geneSetID = 3, #can choose multiple signatures to overlay in this plot
# pvalue_table = FALSE, #can set this to FALSE for a cleaner plot
# title = "SCP/TAP Gene Set") #can also turn off this title
# add a variable to this result that matches enrichment direction with phenotype
myGSEA.df <- myGSEA.df %>%
mutate(phenotype = case_when(
NES > 0 ~ "iL3",
NES < 0 ~ "FLF"))
myGSEA.df$ID <- myGSEA.df$ID %>%
word(sep = ',') %>%
#word(sep = '/') %>%
word(sep = ' and')
# create 'bubble plot' to summarize y signatures across x phenotypes
ggplot(myGSEA.df, aes(x=phenotype, y=ID)) +
geom_point(aes(size=setSize, color = NES, alpha=-log10(p.adjust))) +
scale_color_gradient(low="blue", high="red") +
theme_bw()